package zeebe.vertx;

import io.zeebe.client.ZeebeClient;
import io.zeebe.client.api.response.DeploymentEvent;
import io.zeebe.client.api.response.WorkflowInstanceEvent;
import io.zeebe.client.api.worker.JobWorker;

import java.util.HashMap;
import java.util.Map;

public class VertxApp {

    public static void main(String[] args) {
        System.out.println("创建Zeebe的连接");
        ZeebeClient client = ZeebeClient.newClientBuilder()
                .brokerContactPoint("localhost:26500")
                .usePlaintext()
                .build();

        System.out.println("部署工作流");
        String bpmnPath = "bpmn/vertxProcess.bpmn";
        DeploymentEvent deployment = client.newDeployCommand()
                .addResourceFromClasspath(bpmnPath)
                .send()
                .join();

        System.out.println("启动工作流");
        String processId = "vertxProcess";
        Map<String, Object> varMap = new HashMap<>();
        varMap.put("email", "2711992339@qq.com");
        varMap.put("register", "2711992339@qq.com");

        WorkflowInstanceEvent wfInstance = client.newCreateInstanceCommand()
                .bpmnProcessId(processId)
                .latestVersion()
                .variables(varMap)
                .send()
                .join();

        //创建jobwork，调用eventbus的verification-code服务发送邮件
        System.out.println("创建jobworker");
        String type = "send-mail";
        VertxJobWorkerHandler handler = new VertxJobWorkerHandler(client);
        JobWorker jobWorker = client.newWorker()
                .jobType(type)
                .handler(handler)
                .open();

        System.out.println("流程结束");
    }
}
